/*
 * SignatureSliceFileHandler_gtest.h
 *
 *  Created on: Nov 25, 2012
 *      Author: m
 */

#ifndef SIGNATURESLICEFILEHANDLER_GTEST_H_
#define SIGNATURESLICEFILEHANDLER_GTEST_H_

#include <string>
#include <gtest/gtest.h>

#include "../logic_layer/SignatureSlicingIndexFile/fileHandler/SignatureSliceFileHandler.h"

/**
 * Test Suite de la clase SignatureSliceFileHandler
 */
namespace SignatureSlicingIndex
{

class SignatureSliceFileHandlerTestFixture: public ::testing::Test
{

public:
    SignatureSliceFileHandlerTestFixture()
    {
        /*
         * Acá lo único que hago es obtener el nombre
         * del TestFixture y del TestCase que se está ejecutando
         * y con eso armo un "filename" del tipo: testname.testcase.tmp
         */

        const ::testing::TestInfo* const test_info =
                ::testing::UnitTest::GetInstance()->current_test_info();

        std::string filename( test_info->name() );
        filename.append( "." );
        filename.append( test_info->test_case_name() );
        filename.append( ".tmp" );

        m_filename = filename;
    }

    virtual ~SignatureSliceFileHandlerTestFixture()
    {
    }

    void SetUp()
    {
    }

    void TearDown()
    {
    }

    /**
     * Devuelve un filename como: testname.testcase.tmp
     *
     * @return un filename predecible para usar en el test
     */
    const char* getFileNameForTest()
    {
        return m_filename.c_str();
    }

    std::string m_filename;
};

/**
 * Test begin
 */
TEST_F( SignatureSliceFileHandlerTestFixture, begin)
{
    SignatureSliceFileHandler handler( this->getFileNameForTest(), true );

    ASSERT_TRUE( handler.begin() == 0 );
}

/**
 * Test Get Slice
 */
TEST_F( SignatureSliceFileHandlerTestFixture, GetSlice)
{
    SignatureSliceFileHandler handler( this->getFileNameForTest(), true );

    typedef SignatureRegister<128, KeyWordID> Slice;
    typedef std::shared_ptr<Slice> Slice_ptr;

    Slice_ptr newSlice( new Slice( handler.begin() ) );

    handler.saveSlice( newSlice );

    auto slice = handler.getSlice( handler.begin() );

    ASSERT_EQ( slice->getID(), handler.begin() );
}

/**
 * Test end
 */
TEST_F( SignatureSliceFileHandlerTestFixture, end)
{
    SignatureSliceFileHandler handler( this->getFileNameForTest(), true );

    typedef SignatureRegister<128, KeyWordID> Slice;
    typedef std::shared_ptr<Slice> Slice_ptr;

    Slice_ptr newSlice( new Slice( handler.begin() ) );

    handler.saveSlice( newSlice );

    newSlice.reset( new Slice( handler.begin() + 1 ) );

    handler.saveSlice( newSlice );

    newSlice.reset( new Slice( handler.begin() + 2 ) );

    handler.saveSlice( newSlice );

    ASSERT_EQ( handler.end(), handler.begin()+3 );
}

/**
 * Test Get Slice with data
 */
TEST_F( SignatureSliceFileHandlerTestFixture, GetSliceWithData)
{
    SignatureSliceFileHandler handler( this->getFileNameForTest(), true );

    typedef SignatureRegister<128, KeyWordID> Slice;
    typedef std::shared_ptr<Slice> Slice_ptr;

    Slice::Signature data( "1111" );

    Slice_ptr newSlice( new Slice( handler.begin(), data ) );

    handler.saveSlice( newSlice );

    auto slice = handler.getSlice( handler.begin() );

    ASSERT_EQ( slice->getSignature(), data );
}

/**
 * Test Get Slice que no existe
 */
TEST_F( SignatureSliceFileHandlerTestFixture, GetNonExistentSlice)
{
    SignatureSliceFileHandler handler( this->getFileNameForTest(), true );

    ASSERT_ANY_THROW( handler.getSlice(1) );
}

}

#endif /* SIGNATURESLICEFILEHANDLER_GTEST_H_ */
